package ferp.android.dialogs;

import android.content.Context;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import ferp.android.GUI;
import ferp.android.R;

/**
 * User: olegs
 * Date: 3/27/13 12:59 PM
 */
public class AnimationTimeDialog extends Dialog
{
  public interface Listener
  {
    void onSaveClick(int requestCode, long value);
  }

  public AnimationTimeDialog(Context context, final Listener listener, final int requestCode)
  {
    super(context, R.layout.dialog_animation_time, R.string.dialog_animation_time_title);

    resolveTextView(R.id.dlg_animation_time_min, VALUE_MIN);
    resolveTextView(R.id.dlg_animation_time_max, VALUE_MAX);
    resolveTextView(R.id.dlg_animation_time_current_text);
    resolveTextView(R.id.dlg_animation_time_current_value_ms);

    currentValue = resolveTextView(R.id.dlg_animation_time_current_value);
    slider = (SeekBar)findViewById(R.id.dlg_animation_time_slider);

    slider.setMax(SLIDER_MAX);

    slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
    {
      @Override
      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
      {
        seekBar.setSecondaryProgress(progress);
        currentValue.setText(String.valueOf(progress * SLIDER_GAP));
      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar)
      {
      }

      @Override
      public void onStopTrackingTouch(SeekBar seekBar)
      {
      }
    });

    resolveButton(R.id.dlg_element_footer_button, R.string.save).setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View view)
      {
        listener.onSaveClick(requestCode, Long.valueOf(currentValue.getText().toString()));
      }
    });
  }

  public void set(long value)
  {
    currentValue.setText(String.valueOf(value));
    slider.setProgress((int) (value / SLIDER_GAP));
    slider.setSecondaryProgress(slider.getProgress());
  }

  private TextView resolveTextView(int id)
  {
    TextView tv = (TextView)findViewById(id);

    tv.setTypeface(GUI.Font.BOLD);

    return tv;
  }

  private TextView resolveTextView(int id, int value)
  {
    TextView tv = resolveTextView(id);

    tv.setText(String.valueOf(value));

    return tv;
  }

  private static final int SLIDER_MAX = 10;
  private static final int SLIDER_GAP = 50;

  private static final int VALUE_MIN = 0;
  private static final int VALUE_MAX = SLIDER_MAX * SLIDER_GAP;

  private TextView currentValue;
  private SeekBar slider;
}
